<?php

namespace App\Http\Resources\User;

use App\Enums\VipLevelEnum;
use App\Enums\GenderEnum;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $member = data_get($this->resource, 'member');

        return [
            'userId' => data_get($this->resource, 'id'),
            'uuid' => data_get($this->resource, 'uuid'),
            'nickname' => data_get($this->resource, 'nickname'),
            'vipLevel' => optional($member)->vip_level ?? 0,
            'vipLevelText' => VipLevelEnum::getText(optional($member)->vip_level ?? 0),
            'vipExpireTime' => optional($member)->vip_expire_time ? optional($member)->vip_expire_time->format('Y-m-d H:i:s') : null,
            'gender' => data_get($this->resource, 'gender', 0),
            'genderText' => GenderEnum::getText(data_get($this->resource, 'gender', 0)),
            'birthday' => data_get($this->resource, 'birthday'),
            'avatar' => data_get($this->resource, 'avatar'),
            'gems' => optional(data_get($this->resource, 'asset'))->coin ?? 0,
        ];
    }
}
